package com.forenow.demo.demo25;

import java.sql.*;

/**
 * 预处理：主要用于执行动态的sql语句
 */
public class PrepareStatementDemo {
    static final String DB_URL = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    static final String USER = "root";
    static final String PASS = "123456";
    private static PreparedStatement pst;
    private static Connection conn;
    private static ResultSet resultSet;
    public static void main(String[] args) {
        String sql="insert into emp(eid,ename,sal) value(?,?,?)";
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            //3.获取PrepareStatement对象，作用：执行动态SQL语句
            pst = conn.prepareStatement(sql);
            pst.setInt(1,5);
            pst.setString(2,"Lora");
            pst.setInt(3,5550);
            int i = pst.executeUpdate();
            System.out.println(i);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
}
